package com.rm.zeyou.pay.wxpay.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rm.zeyou.dto.PhoneNumberDTO;
import com.rm.zeyou.mapper.ApplicationConfigMapper;
import com.rm.zeyou.pay.wxpay.common.WechatJSAPIGetMsgUrlConstants;
import com.rm.zeyou.pay.wxpay.service.WechatJSAPIGetMsgService;
import com.rm.zeyou.pay.wxpay.util.WxLoginUtil;
import com.rm.zeyou.pay.wxpay.vo.DecryptWeChartMsgVO;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.util.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author ZhangKeChen
 * @Date 2022/11/3
 * @Description
 */
@Slf4j
@Service
@Transactional
public class WechatJSAPIGetMsgServiceImpl implements WechatJSAPIGetMsgService {


    //@Resource
    //WxPayEntity wxPayEntity;
    @Resource
    private ApplicationConfigMapper applicationConfigMapper;

    /**
     * @param code 小程序微信获取openid的code
     * @return
     */
    @Override
    public ApiRestResponse decryptWeChatOpenIdMsg(String code) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String accountSecret = applicationConfigMapper.selectOneData("account_secret");
        //用code获取openid请求地址
        String baseAccessTokenUrl = String.format(
                WechatJSAPIGetMsgUrlConstants.GetOpenIdByCode,
                accountAppid,
                accountSecret,
                code);
        JSONObject userInfo = WxLoginUtil.getUserInFor(baseAccessTokenUrl);
        log.info("用户信息：{}", userInfo);
        DecryptWeChartMsgVO decryptWeChartMsgVo = new DecryptWeChartMsgVO();
        decryptWeChartMsgVo.setOpenId(userInfo.getString("openid"));
        return ApiRestResponse.success(decryptWeChartMsgVo);
    }

    /**
     * @param code 小程序微信获取phone的code
     * @return
     */
    @Override
    public ApiRestResponse decryptWeChatPhoneMsg(String code) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String accountSecret = applicationConfigMapper.selectOneData("account_secret");
        //用appid和secret获取access_token请求地址
        String getTokenUrl = String.format(
                WechatJSAPIGetMsgUrlConstants.GetAccessTokenByCode,
                accountAppid,
                accountSecret);

        JSONObject userInfo = WxLoginUtil.getUserInFor(getTokenUrl);
        String accessToken = userInfo.getString("access_token");
        //使用access_token获取手机号码
        String phoneUrl = String.format(
                WechatJSAPIGetMsgUrlConstants.GetPhoneByAccessToken,
                accessToken);
        PhoneNumberDTO Phone = new PhoneNumberDTO();
        Phone.setCode(code);
        String s = JSONArray.toJSONString(Phone);
        String phone = HttpUtil.jsonPost(phoneUrl, s);
        PhoneNumberDTO phoneNumber = JSONArray.parseObject(phone, PhoneNumberDTO.class);
        log.info("微信授权获取的手机是" + phoneNumber);
        String resultPhone = phoneNumber.getPhone_info().getPhoneNumber();
        log.info("手机号码:" + resultPhone);
        DecryptWeChartMsgVO decryptWeChartMsgVo = new DecryptWeChartMsgVO();
        decryptWeChartMsgVo.setTel(resultPhone);
        return ApiRestResponse.success(decryptWeChartMsgVo);
    }
}
